﻿using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Web;

namespace YPF.Common.图片文件与流互转
{
    /// <summary>
    /// byte数组与Image的相互转换
    /// </summary>
    public static class ImageHelper
    {
        /// <summary>
        /// 将图片转为byte[]数组
        /// </summary>
        /// <param name="image">源图片</param>
        /// <returns>byte[]数组</returns>
        public static byte[] ImageToBytes(Image image)
        {
            ImageFormat format = image.RawFormat;
            using (MemoryStream ms = new MemoryStream())
            {
                if (format.Equals(ImageFormat.Jpeg))
                {
                    image.Save(ms, ImageFormat.Jpeg);
                }
                else if (format.Equals(ImageFormat.Png))
                {
                    image.Save(ms, ImageFormat.Png);
                }
                else if (format.Equals(ImageFormat.Bmp))
                {
                    image.Save(ms, ImageFormat.Bmp);
                }
                else if (format.Equals(ImageFormat.Gif))
                {
                    image.Save(ms, ImageFormat.Gif);
                }
                else if (format.Equals(ImageFormat.Icon))
                {
                    image.Save(ms, ImageFormat.Icon);
                }
                byte[] buffer = new byte[ms.Length];
                //Image.Save()会改变MemoryStream的Position，需要重新Seek到Begin
                ms.Seek(0, SeekOrigin.Begin);
                ms.Read(buffer, 0, buffer.Length);
                return buffer;
            }
        }

        /// <summary>
        /// 将byte[]数组转为图片
        /// </summary>
        /// <param name="buffer">byte[]数组</param>
        /// <returns>图片</returns>
        public static Image BytesToImage(byte[] buffer)
        {
            MemoryStream ms = new MemoryStream(buffer);
            Image image = System.Drawing.Image.FromStream(ms);
            return image;
        }

        /// <summary>
        /// 将byte[]数组转为图片并保存到指定路径
        /// </summary>
        /// <param name="fileName">保存图片路径</param>
        /// <param name="buffer">源byte[]数组</param>
        /// <returns>保存后的图片全路径地址</returns>
        public static string CreateImageFromBytes(string fileName, byte[] buffer)
        {
            string file = fileName;
            Image image = BytesToImage(buffer);
            ImageFormat format = image.RawFormat;
            if (format.Equals(ImageFormat.Jpeg))
            {
                file += ".jpeg";
            }
            else if (format.Equals(ImageFormat.Png))
            {
                file += ".png";
            }
            else if (format.Equals(ImageFormat.Bmp))
            {
                file += ".bmp";
            }
            else if (format.Equals(ImageFormat.Gif))
            {
                file += ".gif";
            }
            else if (format.Equals(ImageFormat.Icon))
            {
                file += ".icon";
            }
            System.IO.FileInfo info = new System.IO.FileInfo(file);
            System.IO.Directory.CreateDirectory(info.Directory.FullName);
            File.WriteAllBytes(file, buffer);
            return file;
        }

        ///// <summary>
        ///// 参数是Byte[] 类型，没有返回值（ASP.NET输出图片）
        ///// </summary>
        ///// <param name="streamByte"></param>
        //public void WritePhoto(byte[] streamByte)
        //{
        //    // Response.ContentType 的默认值为默认值为“text/html”  
        //    Response.ContentType = "image/GIF";
        //    //图片输出的类型有: image/GIF     image/JPEG  
        //    Response.BinaryWrite(streamByte);
        //}
    }
}
